package com.shopcart.action;

import java.util.Collection;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.demo.annotation.LoginFilter;
import com.demo.bean.DataTable;
import com.demo.bean.Ret;
import com.demo.controller.CoreAdminController;
import com.demo.util.JSONUtils;
import com.demo.util.StringUtil;
import com.demo.validator.AdminLoginValidate;
import com.shopcart.domain.StoreCategory;
import com.shopcart.service.StoreCategoryService;
import com.vsd.repository.StoreCategoryRepository;

@Controller
@RequestMapping("storeCategory")
@LoginFilter(validateClz=AdminLoginValidate.class)
public class StoreCategoryController extends CoreAdminController{
	@Autowired
	private StoreCategoryRepository storeCategoryRepository;
	@Autowired
	private StoreCategoryService storeCategoryService;
 
	
	@RequestMapping("/list")
	@ResponseBody
	public String storeList(HttpSession session,@RequestParam("draw") Integer draw
			,@RequestParam("start") Integer start,@RequestParam("length") Integer length
			,@RequestParam("search[value]") String search,HttpServletRequest request){
		
		Page<StoreCategory> list = storeCategoryService.findAll(new PageRequest(start/length, length,new Sort(Direction.DESC, "sort")));
		
		return JSONUtils.serialize(new DataTable(draw+1,Integer.parseInt(list.getTotalElements()+""), list.getContent()));
	}
	
	@RequestMapping("/listByLevel")
	@ResponseBody
	public String listByLevel(HttpSession session,Integer level,HttpServletRequest request){
		
		Collection<StoreCategory> p= storeCategoryService.findByLevel(1,new Sort(Direction.DESC, "sort"));
		
		return JSONUtils.serialize(p);
	}
	
	@RequestMapping("/save")
	@ResponseBody
	public String save(StoreCategory cate,HttpServletRequest request){
		if(StringUtils.isBlank(cate.getId())){
			cate.setId(StringUtil.getUUID());
			cate.setCreateTime(new Date());
			cate.setParent("0");
			
			storeCategoryService.save(cate);
		}else{
			cate.setCode(null);
			cate.setLevel(null);
			cate.setParent(null);
			storeCategoryService.updateByPrimaryKey(cate);
		}
		return JSONUtils.serialize(new Ret(0));
	}
	
	@RequestMapping("/findById")
	@ResponseBody
	public String findById(String id){
		 StoreCategory s = storeCategoryService.findById(id);
		return JSONUtils.serialize(s);
	}
	
	
	@RequestMapping("/del")
	@ResponseBody
	public String del(String id){
		String[] ids = id.split(",");
		for (String string : ids) {
			storeCategoryService.delete(string);	
		}
		return JSONUtils.serialize(new Ret(0));
	}
	
	@RequestMapping("/unique")
	@ResponseBody
	public String unique(String code){
		int c = storeCategoryService.countByCode(code);
		JSONObject ret = new JSONObject();
		if(c>0) ret.put("valid", false);
		else ret.put("valid", true);
		return ret.toString();
	}
	
	
}
